package com.example.infowall02.controller;

import com.example.infowall02.service.impl.EmailService;
import com.example.infowall02.service.impl.VerificationCodeService;
import com.example.infowall02.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

// 发送验证码
@RestController
public class VerificationCodeController {
    // 注入邮件发送器
    @Autowired
    private JavaMailSender javaMailSender;
    // 注入邮件服务
    @Autowired
    private EmailService emailService;
    // 注入验证码服务
    @Autowired
    private VerificationCodeService verificationCodeService;
    // 发送验证码
    @PostMapping("/sendVerificationCode")
    public Result sendVerificationCode(@RequestParam String email) {
        try {
            // 生成邮箱验证码
            String code = verificationCodeService.generateVerificationCode();
            // 保存验证码
            verificationCodeService.saveVerificationCode(email, code);
            // 发送邮箱验证码
            emailService.sendVerificationCode(email, code);
            return Result.success("验证码发送成功");
        } catch (Exception e) {
            // 记录异常信息到日志
            e.printStackTrace();
            return Result.error("验证码发送失败"+e.getMessage());
        }
    }
}

